/*
 * ArcEmu MMORPG Server
 * Copyright (C) 2008-2009 <http://www.ArcEmu.org/>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#ifndef _UTIL_H
#define _UTIL_H

#include "Common.h"
#include "String.h"

class Vector3;
//class String;

namespace Util {
///////////////////////////////////////////////////////////////////////////////
// String Functions ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
std::vector<std::string> StrSplit(const std::string &src, const std::string &sep);

// This HAS to be called outside the threads __try / __except block!
void SetThreadName(const char* format, ...);
time_t convTimePeriod ( uint32 dLength, char dType);

inline uint32 secsToTimeBitFields(time_t secs)
{
    tm lt;
    localtime_r(&secs,&lt);
    return (lt.tm_year - 100) << 24 | lt.tm_mon  << 20 | (lt.tm_mday - 1) << 14 | lt.tm_wday << 11 | lt.tm_hour << 6 | lt.tm_min;
}

inline String secsToTimeString(time_t secs)
{
	tm lt;
	localtime_r(&secs,&lt);
	char  timeStr[20] = "";
	sprintf(timeStr,"%04d-%02d-%02d %02d:%02d:%02d",lt.tm_year + 1900,lt.tm_mon + 1,lt.tm_mday,lt.tm_hour,lt.tm_min,lt.tm_sec);
	return String(timeStr);
}


extern SERVER_DECL const char* _StringToUTF8(const char*pASCIIBuf);
extern SERVER_DECL const char* _StringToANSI(const char*pUtf8Buf);
extern SERVER_DECL bool _IsStringUTF8(const char *str);

extern SERVER_DECL uint8  GetDirection(const Vector3 & src,const Vector3 & dest);
extern SERVER_DECL float  GetNewX(const uint8 & dir, float x);
extern SERVER_DECL float  GetNewY(const uint8 & dir, float y);
extern SERVER_DECL float  GetNewZ(const uint8 & dir, float z);
extern SERVER_DECL Vector3  GetNewPos(const uint8 & dir ,const Vector3 & pos);


//string fcs
String lastDir(const String &s);
String lastFile(const String &s);
String cutLastFile(const String &s);
String cutLastExt(const String &s);
String ext(const String &s);
String replaceBy(const String &s, char c1, char c2);
String toLower(const String &s);
void copyStringToBuffer(char *buffer, int bufferSize, const String& s);

//numeric fcs
int clamp(int value, int min, int max);
float clamp(float value, float min, float max);
float saturate(float value);
int round(float f);

//misc
bool fileExists(const String &path);

template<typename T>
void deleteValues(T beginIt, T endIt){
        for(T it= beginIt; it!=endIt; ++it){
                delete *it;
        }
}

template<typename T>
void deleteMapValues(T beginIt, T endIt){
        for(T it= beginIt; it!=endIt; ++it){
                delete it->second;
        }
}

#ifdef WIN32

	typedef struct tagTHREADNAME_INFO
	{
		DWORD dwType; // must be 0x1000
		LPCSTR szName; // pointer to name (in user addr space)
		DWORD dwThreadID; // thread ID (-1=caller thread)
		DWORD dwFlags; // reserved for future use, must be zero
	} THREADNAME_INFO;

#endif

}

#endif
